<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
</head>

<body>
  <!-- 使用Vue.set/this.$set修改数据 -->
  <div id="app">
    <span>姓名是{{person.name}}</span>
    <span>年龄是{{person.age}}</span>
    <button @click="handleAgeClick">修改年龄</button>
    <button @click="handleArrClick">修改数组</button>
    <ul>
      <li v-for="item in this.arr">
        {{item}}
      </li>
    </ul>
  </div>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        person: {
          name: 'lishi'
        },
        arr: ['arr1', 'arr2', 'arr3', 'arr4']
      },
      methods: {
        handleAgeClick() {
          // this.person.name = 'wangwu' //修改属性
          this.person.age = 21 //设置新属性,无效的形式
          // this.$set(this.person, 'age', 23)
          //Vue.set(app.person,'age',45)
          // this.person = Object.assign({}, this.person, { age: 90 })
        },
        handleArrClick() {
          // this.arr[2] = 'changearr' //修改数组元素，无效的形式
          // this.$set(this.arr, 2, 'changearr2')
          // this.arr.push('arr5')
          this.arr.splice(2, 1, 'newarr')
        }
      }
    })
  </script>
</body>
</html>